<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>冒泡和快速排序</title>
</head>
<body>
  
<script>

const arr = [5, 3, 8, 4, 2, 7, 1, 10, 6, 9]

// 冒泡排序
function bubbleSort(arr) {
  for(let i = 0; i< arr.length; i++) {
    for(let j = i + 1; j < arr.length; j++) {
        if(arr[i] > arr[j]) {
          var bar = arr[i]
          arr[i] = arr[j]
          arr[j] = bar
        }
    }
  }
  return arr
}

console.log('冒泡排序',bubbleSort(arr))

// 快速排序
function quickSort(arr) {
  if(arr.length <= 1) {
    return arr
  }
  let pivotIndex = Math.floor(arr.length / 2)
  let pivot = arr.splice(pivotIndex, 1)[0]
  let left = []
  let right = []
  for(let i = 0; i < arr.length; i++) {
    if(arr[i] < pivot) {
      left.push(arr[i])
    } else {
      right.push(arr[i])
    }
  }
  return quickSort(left).concat([pivot], quickSort(right))
}

console.log('快速排序',quickSort(arr))

</script>

</body>
</html>